package math.spbu.ru.historycharts;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import math.spbu.ru.historycharts.IDataProvider;
import math.spbu.ru.historycharts.model.DatePoint;
import math.spbu.ru.historycharts.model.XYMultipleSeriesDataset;
import math.spbu.ru.historycharts.model.XYSeries;
import math.spbu.ru.historycharts.renderer.PointStyle;
import math.spbu.ru.historycharts.renderer.XYMultipleSeriesRenderer;
import math.spbu.ru.historycharts.renderer.XYSeriesRenderer;

import android.graphics.Color;
import android.util.Log;

public class DataProvider implements IDataProvider{

	private XYMultipleSeriesRenderer mainRenderer;
	private XYMultipleSeriesDataset  mainDataset;
	private ChartsActivity ca;
	private String rateTypeID;
	private Calendar dateStart, dateEnd;


		public DataProvider(){			
			dateEnd = new GregorianCalendar();
			dateStart = new GregorianCalendar();
			dateStart.set(Calendar.DAY_OF_MONTH,
					dateStart.get(Calendar.DAY_OF_MONTH) - 30);
			

			ArrayList<ArrayList<DatePoint>> dateList = new ArrayList<ArrayList<DatePoint>>();
			dateList.add((ArrayList<DatePoint>) getDinamicRateList(
					dateStart.getTimeInMillis(), dateEnd.getTimeInMillis()));
			mainDataset = buildDateDataset(getTitles(), dateList);
			mainRenderer = buildRenderer(getTitles().size());
		}

	public DataProvider(ChartsActivity ca, String RateTypeID){
		this.ca = ca;
		this.rateTypeID = RateTypeID;
		
		dateEnd = new GregorianCalendar();
		dateStart = new GregorianCalendar();
		dateStart.set(Calendar.DAY_OF_MONTH,
				dateStart.get(Calendar.DAY_OF_MONTH) - 30);
		
		ArrayList<ArrayList<DatePoint>> dateList = new ArrayList<ArrayList<DatePoint>>();
		dateList.add((ArrayList<DatePoint>) getDinamicRateList(dateStart.getTimeInMillis(), dateEnd.getTimeInMillis()));
		mainDataset = buildDateDataset(getTitles(),dateList);
		mainRenderer = buildRenderer(getTitles().size());	
		
		
		
	//	dateStart.set(Calendar., value)
	}

	public List<DatePoint> getDinamicRateList(int count)
	{		
		//		ProviderManager provider = ProviderManager.getInstance(ca);
		//		Calendar endDate = new GregorianCalendar();
		//		Calendar startDate = new GregorianCalendar();
		//		startDate.set(Calendar.DAY_OF_MONTH, startDate.get(Calendar.DAY_OF_MONTH) - 21);
		//
		//		ArrayList<DatePoint> temp = convertToListDatePoint(provider.getDynamicRates(
		//				rateTypeID, startDate.getTime(), endDate.getTime()));
		//
		//		List<String> titles = new ArrayList<String>();
		//		titles.add("Chart1");
		//
		//		return temp;

		//******************************************	


		List<DatePoint> datePoints = new ArrayList<DatePoint>(count);

		Calendar date = new GregorianCalendar();

		for (int i = 0; i < count; i++) {
			datePoints.add(new DatePoint((Calendar) date.clone(), (float) (Math.random() * 100)));
			date.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH) + 1);
		}
		return datePoints;


	}

	public List<DatePoint> getDinamicRateList(long start, long end) {
		//ProviderManager provider = ProviderManager.getInstance(ca);
		//
		//		Calendar endDate = new GregorianCalendar();
		//		Calendar startDate = new GregorianCalendar();
		//		startDate.setTimeInMillis(start);
		//		endDate.setTimeInMillis(end);
		//
		//		ArrayList<DatePoint> temp = convertToListDatePoint(provider.getDynamicRates(
		//				rateTypeID, startDate.getTime() , endDate.getTime()));
		//
		//
		//		return temp;
		//******************************************	

		List<DatePoint> datePoints = new ArrayList<DatePoint>();

		Calendar date = new GregorianCalendar();
		date.setTimeInMillis(start);

		for (int i = 0; date.getTimeInMillis() < end; i++) {
			date.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH) + 1);
			datePoints.add(new DatePoint((Calendar) date.clone(), (float) (Math.random() * 100)));
		}
		return datePoints;
	}

	@Override
	public List<String> getTitles() {
		// TODO Auto-generated method stub
		ArrayList<String> al = new ArrayList<String>(1);
		al.add("chart1");
		return al;
	}

	public XYMultipleSeriesRenderer getRenderer(){
		return mainRenderer;
	}

	public XYMultipleSeriesDataset getDataset(){
		return mainDataset;
	}

	/**
	 * Builds an XY multiple time dataset using the provided values.
	 */
	public XYMultipleSeriesDataset buildDateDataset(List<String> titles, ArrayList<ArrayList<DatePoint>> datePoints) {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		int length = titles.size();
		for (int i = 0; i < length; i++) {
			XYSeries series = new XYSeries(titles.get(i));
			List<DatePoint> dpList = datePoints.get(i);		
			series.addListInEnd(dpList);
			dataset.addSeries(series);
		}
		return dataset;
	}

	/**
	 * Builds an XY multiple series renderer
	 */
	public XYMultipleSeriesRenderer buildRenderer(int count) {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		for (int i = 0; i < count; i++) {
			XYSeriesRenderer r = new XYSeriesRenderer();
			r.setColor(getRandomColor());
			r.setPointStyle(PointStyle.CIRCLE);
			r.setFillPoints(true);
			renderer.addSeriesRenderer(r);
		}
		renderer.setXAxisMax(getDataset().getMaxX());
		renderer.setXAxisMin(getDataset().getMinX());

		return renderer;
	}

	//TODO
	private int getRandomColor(){
		return Color.RED;
	}

//	private ArrayList<DatePoint> convertToListDatePoint(List<Rate> in) {
//		ArrayList<DatePoint> out = new ArrayList<DatePoint>(in.size());
//		for (Rate r : in) {
//			out.add(new DatePoint(r.getDate(), r.getValue()));
//		}
//		return out;
//
//	}

	public String getDateFormat(){
		return DATA_FORMAT;		
	}

	public void setDateFormat(String format){
		DATA_FORMAT = format;		
	}

	private String DATA_FORMAT = "dd MMM";


	@Override
	public DatePoint getDateValueByXY(double x, double y) {
		DatePoint date = null;
			
		XYSeries d = mainDataset.getSeriesAt(0);
		int first = d.getFirst(), last = d.getLast();	
		float[] vector = d.getPointsVector();
		double eps = 5;
		Log.d("BNV", "x= "+x+" y= "+y);
			for (int i = first*2; (i < last*2) && (vector[i] < x + eps); i+=2) {
				Log.d("BNV", "i= "+i+" v[i]= "+vector[i]+" "+vector[i+1]);
				if ((vector[i] < x + eps) && (vector[i] > x - eps)) 
					if ((vector[i+1] < y + eps) && (vector[i+1] > y - eps)) 
						date = new DatePoint(d.getX(i), d.getY(i));
			}
		return date;
	}
}
